package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.common.exception.CustException;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dto.ChannelDto;
import com.heima.model.admin.pojo.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service//业务层
@Slf4j
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {
    //用哪个mapper,哪个实体类
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {

        dto.checkParam();
        //检测参数是否正确

        //封装查询条件   查询参数   分页参数

        IPage<AdChannel> pageRequest = new Page<>(dto.getPage(), dto.getSize());//分页的参数
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.<AdChannel>lambdaQuery();//构造查询条件
        //判断name是否为空,不为空才要构造条件
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AdChannel::getName, dto.getName());
        }


        //执行查询得到结果
        IPage<AdChannel> pageResult = this.page(pageRequest, queryWrapper);//进行分页查询

        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal());//分页的基本参数

        //查询到的集合
        pageResponseResult.setData(pageResult.getRecords());//查询到的集合

        return pageResponseResult;
    }

    /**
     * 新增
     *
     * @param adChannel
     * @return 优化：前端添加是否默认
     */
    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1.参数检验
        if (StringUtils.isBlank(adChannel.getName()))//用于判断字符串,比isempty 更严格,  判断 对象有没有
        {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能为空");
        }



        //执行新增
        //查询原有的默认频道,更新为非默认
        //先非空判断,在判断频道是否是默认状态
        if (adChannel.getIsDefault()!=null&&adChannel.getIsDefault()){//如果新增的是默认状态,
            //查询原有的默认,再修改默认为非默认
            LambdaQueryWrapper<AdChannel> wrapper = Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getIsDefault, true);
            List<AdChannel> list = list(wrapper);
            list.forEach((channel)->{
                channel.setIsDefault(false);
                updateById(channel);
            });
        }
        adChannel.setCreatedTime(new Date());//设置创建时间
        save(adChannel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
        //先根据id查频道有没有,在看频道状态是否是启用的

        //自定义异常类
        if (id==5){//值用双等
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"id不能等于5");
            //工具类,已经new  异常类的对象

        }

        AdChannel adChannel = this.getById(id);//查询

        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不存在");
        }

        if (adChannel.getStatus()!=null&&adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"频道已经启用,不允许操作");
        }

        this.removeById(id);
        return ResponseResult.okResult();
    }

}
